package webapp.mapperScan.mapperLoader;


import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils;
import webapp.mapperScan.annotation.DemoMapperScan;
import webapp.mapperScan.pathScan.DemoClassPathScan;
import webapp.xmlEntity.XmlMapper;

import java.util.ArrayList;
import java.util.List;

public class DemoMapperLoader implements ResourceLoaderAware , ImportBeanDefinitionRegistrar {

    public static List<XmlMapper> xmlObjLists = new ArrayList<>();
    private ResourceLoader resourceLoader;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        AnnotationAttributes annoAttr = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(DemoMapperScan.class.getName()));


        String[] packages = StringUtils.delimitedListToStringArray(annoAttr.getString("packages"),",");

        DemoClassPathScan demoClassPathScan = new DemoClassPathScan(registry);
        demoClassPathScan.registerFilters();
        demoClassPathScan.scan(packages);


    }
}
